﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class AddOrUpdateEventViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        private GWCmdMakeUpService gWCmdMakeUpService = new GWCmdMakeUpService();
        private int totalNum;

        public int TotalNum
        {
            get { return totalNum; }
            set { totalNum = value; RaisePropertyChanged(); GetDataInfo(value); }
        }

        private List<int> totalNumList;

        public List<int> TotalNumList
        {
            get { return totalNumList; }
            set { totalNumList = value; RaisePropertyChanged(); }
        }


        private string number;

        public string Number
        {
            get { return number; }
            set { number = value; RaisePropertyChanged(); }
        }

        private string deep;

        public string Deep
        {
            get { return deep; }
            set { deep = value; RaisePropertyChanged(); }
        }

        private int typeSelected;

        public int TypeSelected
        {
            get { return typeSelected; }
            set { typeSelected = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<string> typeList;

        public ObservableCollection<string> TypeList
        {
            get { return typeList; }
            set { typeList = value; RaisePropertyChanged(); }
        }

        private string msData;

        public string MSData
        {
            get { return msData; }
            set { msData = value; RaisePropertyChanged(); }
        }

        private int reportFlagSelected;

        public int ReportFlagSelected
        {
            get { return reportFlagSelected; }
            set { reportFlagSelected = value; RaisePropertyChanged(); }
        }
            
        private ObservableCollection<string> reportFlagList;

        public ObservableCollection<string> ReportFlagList
        {
            get { return reportFlagList; }
            set { reportFlagList = value; RaisePropertyChanged(); }
        }

        private string groupName;

        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; RaisePropertyChanged(); }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); GroupName = $"第{value + 1}个配置单元"; UpDataInfo(); }
        }

        private ObservableCollection<GWCmdMakeUp> gWCmdMakeUpList;

        public ObservableCollection<GWCmdMakeUp> GWCmdMakeUpList
        {
            get { return gWCmdMakeUpList; }
            set { gWCmdMakeUpList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<RCSDDataModel> rOADList;

        public ObservableCollection<RCSDDataModel> ROADList
        {
            get { return rOADList; }
            set { rOADList = value; RaisePropertyChanged(); }
        }


        public AddOrUpdateEventViewModel()
        {
            TotalNumList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            TypeList = new ObservableCollection<string>() { "周期采集事件数据","根据通知采集数据所有事件数据","根据通知采集指定事件数据"};
            ReportFlagList = new ObservableCollection<string>() { "False:不上报","True:立即上报"};
            ROADList = new ObservableCollection<RCSDDataModel>() { };
        }


        public void GetDataInfo(int total)
        {
            GWCmdMakeUpList = new ObservableCollection<GWCmdMakeUp>(gWCmdMakeUpService.SelectByOAD("60167F00", total));
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            SelectedIndex = 0;
            GWCmdMakeUp = null;

        }
        public void UpDataInfo()
        {
            if (SelectedIndex < 0)
            {
                SelectedIndex = 0;
            }
            if (GWCmdMakeUpList.Count <= 0)
            {
                return;
            }
            //判断之前是否点击过，如果点击过则保存之前的数据
            if (GWCmdMakeUp != null)
            {
                SaveDataInfo();
            }

            GWCmdMakeUp = GWCmdMakeUpList[SelectedIndex];
            if (GWCmdMakeUp == null)
            {
                return;
            }
            try
            {
                ROADList.Clear();
                var datas = GWCmdMakeUp.DataValue.Split(';');
                if (datas.Length < 4)
                {
                    return;
                }
                if (datas.Length==6)
                {
                    Number = datas[0];
                    TypeSelected = int.Parse(datas[1]);
                    var strings = datas[2].Split(',');
                    for (int i = 0; i < strings.Length; i++)
                    {
                        RCSDDataModel rCSDDataModel = new RCSDDataModel();
                        rCSDDataModel.Data = strings[i];
                        ROADList.Add(rCSDDataModel);
                    }
                    MSData = datas[3];
                    ReportFlagSelected = int.Parse(datas[4]);
                    Deep = datas[5];
                }
                else
                {
                    Number = datas[0];
                    TypeSelected = int.Parse(datas[1]);
                    MSData = datas[2];
                    ReportFlagSelected = int.Parse(datas[3]);
                    Deep = datas[4];
                }
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        public void SaveDataInfo()
        {
            if (GWCmdMakeUp == null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(int.Parse(Number).ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(TypeSelected.ToString());
            stringBuilder.Append(";");
            for (int i = 0; i < ROADList.Count; i++)
            {
                stringBuilder.Append(ROADList[i].Data);
                if (i+1< ROADList.Count)
                {
                    stringBuilder.Append(",");
                }
            }
            stringBuilder.Append(";");
            stringBuilder.Append(MSData);
            stringBuilder.Append(";");
            stringBuilder.Append(ReportFlagSelected.ToString());
            stringBuilder.Append(";");
            stringBuilder.Append(int.Parse(Deep).ToString());
            
            GWCmdMakeUp.DataValue = stringBuilder.ToString();
            gWCmdMakeUpService.Update(GWCmdMakeUp);
        }

        public RelayCommand MSDataCommand
        {
            get
            {
                var command = new RelayCommand(() =>
                {
                    MSDataModel mSDataModel = new MSDataModel();
                    mSDataModel.Data = MSData;
                    SimpleIoc.Default.Unregister<MSDataModel>();
                    SimpleIoc.Default.Unregister<MeterListViewModel>();
                    GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register<MeterListViewModel>();
                    GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register(() => mSDataModel);
                    MeterListView meterListView = new MeterListView();
                    if (meterListView.ShowDialog() == true)
                    {
                        MSData = mSDataModel.Data;
                    }
                });
                return command;
            }
        }
    }
}
